import Const from './const'

function Data() {
  return {
    set: set,
    get: get,
    remove: remove,
    clear: clear
  }


  function getKey(key) {
    return Const.DATA.KEY_PREFIX + key
  }

  function get(key) {
    if (key) {
      key = getKey(key)
      return JSON.parse(window.localStorage.getItem(key))
    }
  }

  function set(key, val) {
    if (key) {
      key = getKey(key)
      return window.localStorage.setItem(key, JSON.stringify(val))
    }
  }

  function remove(key) {
    if (key) {
      key = getKey(key)
      return window.localStorage.removeItem(key)
    }
  }

  function clear(key) {
    window.localStorage.clear()
  }
}

export default new Data()
